package com.wbs.shop.nuby.view.adapter;

import android.content.Context;

import androidx.recyclerview.widget.RecyclerView;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.wbs.shop.nuby.R;
import com.wbs.shop.nuby.bean.GoodsShowBean;
import com.wbs.shop.nuby.utils.GlideUtils;
import com.wbs.shop.nuby.utils.Logger;
import com.wbs.shop.nuby.utils.StringUtils;
import com.wbs.shop.nuby.view.base.BaseRecyclerAdapter;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by zhoucl on 2018/6/15 0015.
 */
public class GoodsShowAdapter extends BaseRecyclerAdapter<GoodsShowAdapter.ViewHolder, GoodsShowBean> {
    private double discount = 1;

    public GoodsShowAdapter(Context mContext) {
        super(mContext);
    }

    @Override
    public int getItemLoayoutId() {
        return R.layout.item_order_show;
    }

    @Override
    public ViewHolder onCreateViewHolder(View rootView, int viewType) {
        return new ViewHolder(rootView);
    }

    public void changeDiscount(double discount) {
        if (this.discount != discount) {
            this.discount = discount;
            notifyDataSetChanged();
        }
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, GoodsShowBean bean, int position) {
        GlideUtils.load(mContext, bean.getImgUrl(), holder.ivIcon);
        holder.tvName.setText(bean.getName());
        holder.tvBottomName.setVisibility(StringUtils.isNotEmpty(bean.getBottomName()) ? View.VISIBLE : View.GONE);
        holder.tvBottomName.setText(mContext.getString(R.string.text_spec, bean.getBottomName()));
//        holder.tvMoney.setText(mContext.getString(R.string.text_money, bean.getPrice()));
        try {
            holder.tvMoney.setText(mContext.getString(R.string.text_money, new BigDecimal(bean.getPrice()).multiply(new BigDecimal(discount)).setScale(2, RoundingMode.HALF_UP).toEngineeringString()));
        }catch (Exception e){
            Logger.e(e);
        }
        holder.tvNumber.setText(String.format(Locale.CHINESE, "x%d", bean.getQuantity()));
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.ivIcon)
        ImageView ivIcon;
        @BindView(R.id.tvMoney)
        TextView tvMoney;
        @BindView(R.id.tvName)
        TextView tvName;
        @BindView(R.id.tv_number)
        TextView tvNumber;
        @BindView(R.id.tvBottomName)
        TextView tvBottomName;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
